using System;
using Volo.Abp.Domain.Entities.Auditing;
using Volo.Abp.MultiTenancy;

namespace UserManagementService.Users
{
    /// <summary>
    /// 用户活动日志实体
    /// </summary>
    public class UserActivityLog : FullAuditedEntity<Guid>, IMultiTenant
    {
        /// <summary>
        /// 租户ID
        /// </summary>
        public virtual Guid? TenantId { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public virtual Guid UserId { get; protected set; }

        /// <summary>
        /// 操作类型
        /// </summary>
        public virtual ActivityType ActivityType { get; protected set; }

        /// <summary>
        /// 操作描述
        /// </summary>
        public virtual string Description { get; protected set; }

        /// <summary>
        /// 请求URL
        /// </summary>
        public virtual string? RequestUrl { get; protected set; }

        /// <summary>
        /// 请求方法（GET、POST、PUT、DELETE等）
        /// </summary>
        public virtual string? RequestMethod { get; protected set; }

        /// <summary>
        /// IP地址
        /// </summary>
        public virtual string? IpAddress { get; protected set; }

        /// <summary>
        /// 用户代理
        /// </summary>
        public virtual string? UserAgent { get; protected set; }

        /// <summary>
        /// 浏览器信息
        /// </summary>
        public virtual string? BrowserInfo { get; protected set; }

        /// <summary>
        /// 操作系统信息
        /// </summary>
        public virtual string? OperatingSystem { get; protected set; }

        /// <summary>
        /// 设备信息
        /// </summary>
        public virtual string? DeviceInfo { get; protected set; }

        /// <summary>
        /// 位置信息
        /// </summary>
        public virtual string? Location { get; protected set; }

        /// <summary>
        /// 操作结果（0:成功 1:失败）
        /// </summary>
        public virtual ActivityResult Result { get; protected set; }

        /// <summary>
        /// 错误信息（当操作失败时）
        /// </summary>
        public virtual string? ErrorMessage { get; protected set; }

        /// <summary>
        /// 操作时长（毫秒）
        /// </summary>
        public virtual long? Duration { get; protected set; }

        /// <summary>
        /// 客户端时间戳
        /// </summary>
        public virtual DateTime? ClientTimestamp { get; protected set; }

        /// <summary>
        /// 额外的元数据（JSON格式）
        /// </summary>
        public virtual string? Metadata { get; protected set; }

        /// <summary>
        /// 会话ID
        /// </summary>
        public virtual string? SessionId { get; protected set; }

        /// <summary>
        /// 关联实体ID（如操作的对象ID）
        /// </summary>
        public virtual Guid? RelatedEntityId { get; protected set; }

        /// <summary>
        /// 关联实体类型
        /// </summary>
        public virtual string? RelatedEntityType { get; protected set; }

        protected UserActivityLog()
        {
        }

        /// <summary>
        /// 创建用户活动日志
        /// </summary>
        public UserActivityLog(
            Guid id,
            Guid userId,
            ActivityType activityType,
            string description,
            ActivityResult result = ActivityResult.Success,
            Guid? tenantId = null) : base(id)
        {
            UserId = userId;
            ActivityType = activityType;
            Description = Check.NotNullOrWhiteSpace(description, nameof(description), maxLength: 1000);
            Result = result;
            TenantId = tenantId;
        }

        /// <summary>
        /// 设置请求信息
        /// </summary>
        public void SetRequestInfo(string? requestUrl, string? requestMethod)
        {
            RequestUrl = Check.Length(requestUrl, nameof(requestUrl), maxLength: 2048);
            RequestMethod = Check.Length(requestMethod, nameof(requestMethod), maxLength: 16);
        }

        /// <summary>
        /// 设置网络信息
        /// </summary>
        public void SetNetworkInfo(string? ipAddress, string? userAgent)
        {
            IpAddress = Check.Length(ipAddress, nameof(ipAddress), maxLength: 64);
            UserAgent = Check.Length(userAgent, nameof(userAgent), maxLength: 1024);
        }

        /// <summary>
        /// 设置客户端信息
        /// </summary>
        public void SetClientInfo(string? browserInfo, string? operatingSystem, string? deviceInfo, string? location)
        {
            BrowserInfo = Check.Length(browserInfo, nameof(browserInfo), maxLength: 512);
            OperatingSystem = Check.Length(operatingSystem, nameof(operatingSystem), maxLength: 256);
            DeviceInfo = Check.Length(deviceInfo, nameof(deviceInfo), maxLength: 256);
            Location = Check.Length(location, nameof(location), maxLength: 256);
        }

        /// <summary>
        /// 设置操作结果
        /// </summary>
        public void SetResult(ActivityResult result, string? errorMessage = null)
        {
            Result = result;
            ErrorMessage = result == ActivityResult.Success
                ? null
                : Check.Length(errorMessage, nameof(errorMessage), maxLength: 1000);
        }

        /// <summary>
        /// 设置操作时长
        /// </summary>
        public void SetDuration(long duration)
        {
            Duration = duration > 0 ? duration : null;
        }

        /// <summary>
        /// 设置客户端时间戳
        /// </summary>
        public void SetClientTimestamp(DateTime? clientTimestamp)
        {
            ClientTimestamp = clientTimestamp;
        }

        /// <summary>
        /// 设置元数据
        /// </summary>
        public void SetMetadata(string? metadata)
        {
            Metadata = Check.Length(metadata, nameof(metadata), maxLength: 4000);
        }

        /// <summary>
        /// 设置会话ID
        /// </summary>
        public void SetSessionId(string? sessionId)
        {
            SessionId = Check.Length(sessionId, nameof(sessionId), maxLength: 128);
        }

        /// <summary>
        /// 设置关联实体信息
        /// </summary>
        public void SetRelatedEntity(Guid? entityId, string? entityType)
        {
            RelatedEntityId = entityId;
            RelatedEntityType = Check.Length(entityType, nameof(entityType), maxLength: 128);
        }

        /// <summary>
        /// 标记操作成功
        /// </summary>
        public void MarkSuccess()
        {
            SetResult(ActivityResult.Success);
        }

        /// <summary>
        /// 标记操作失败
        /// </summary>
        public void MarkFailure(string errorMessage)
        {
            SetResult(ActivityResult.Failure, errorMessage);
        }
    }

    /// <summary>
    /// 用户活动类型枚举
    /// </summary>
    public enum ActivityType
    {
        /// <summary>
        /// 登录
        /// </summary>
        Login = 0,

        /// <summary>
        /// 登出
        /// </summary>
        Logout = 1,

        /// <summary>
        /// 注册
        /// </summary>
        Register = 2,

        /// <summary>
        /// 更新资料
        /// </summary>
        UpdateProfile = 3,

        /// <summary>
        /// 修改密码
        /// </summary>
        ChangePassword = 4,

        /// <summary>
        /// 重置密码
        /// </summary>
        ResetPassword = 5,

        /// <summary>
        /// 启用账户
        /// </summary>
        EnableAccount = 6,

        /// <summary>
        /// 禁用账户
        /// </summary>
        DisableAccount = 7,

        /// <summary>
        /// 分配角色
        /// </summary>
        AssignRole = 8,

        /// <summary>
        /// 移除角色
        /// </summary>
        RemoveRole = 9,

        /// <summary>
        /// 加入组织
        /// </summary>
        JoinOrganization = 10,

        /// <summary>
        /// 离开组织
        /// </summary>
        LeaveOrganization = 11,

        /// <summary>
        /// 创建数据
        /// </summary>
        CreateData = 12,

        /// <summary>
        /// 更新数据
        /// </summary>
        UpdateData = 13,

        /// <summary>
        /// 删除数据
        /// </summary>
        DeleteData = 14,

        /// <summary>
        /// 查看数据
        /// </summary>
        ViewData = 15,

        /// <summary>
        /// 导出数据
        /// </summary>
        ExportData = 16,

        /// <summary>
        /// 导入数据
        /// </summary>
        ImportData = 17,

        /// <summary>
        /// 上传文件
        /// </summary>
        UploadFile = 18,

        /// <summary>
        /// 下载文件
        /// </summary>
        DownloadFile = 19,

        /// <summary>
        /// 删除文件
        /// </summary>
        DeleteFile = 20,

        /// <summary>
        /// 其他操作
        /// </summary>
        Other = 99
    }

    /// <summary>
    /// 活动结果枚举
    /// </summary>
    public enum ActivityResult
    {
        /// <summary>
        /// 成功
        /// </summary>
        Success = 0,

        /// <summary>
        /// 失败
        /// </summary>
        Failure = 1,

        /// <summary>
        /// 部分成功
        /// </summary>
        PartialSuccess = 2
    }
}